import VueRouter from 'vue-router'
import {home} from "./modules/home";
import {customer} from "./modules/customer";
import {system} from "./modules/system";
import {product} from "./modules/product";
import {warehousing} from "./modules/warehousing";
import {warehouse} from "./modules/warehouse";
import {orders} from "./modules/orders";
import {warehouse_out} from "./modules/warehouse_out";
import {common} from "./modules/common";
import {loyout} from "./modules/layout";
import store from "../store"


export const router = new VueRouter({
    routes: [{
        path: '/admin',
        name: 'admin',
        component: () => import('../views/Index'),
        children: [
            ...home,
            ...customer,
            ...system,
            ...product,
            ...warehousing,
            ...warehouse_out,
            ...warehouse,
            ...orders
        ]
    },
        ...common,
        ...loyout
    ]
});



router.beforeEach((to, from, next) => {
    let isLogin = store.getters['auth/login'];
    if(to.name === 'login' && Boolean(isLogin)){
        next({name: 'home'})
    }else if(to.name !== 'login' && !Boolean(isLogin) ){
        next({name: 'login'})
    }else{
        next()
    }
});


router.afterEach((to, from) => {
    store.dispatch('layout/open', to);
});

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
};
